package indi.kider.server.woniubus.service;

import com.alibaba.fastjson.JSON;
import com.google.common.base.Strings;
import com.google.common.collect.ImmutableMap;
import indi.kider.common.constant.*;
import indi.kider.common.model.bill.RentBill;
import indi.kider.common.model.employee.TenantQuery;
import indi.kider.common.model.woniubus.TenantExportVo;
import indi.kider.server.apartment.mapper.HousesMapper;
import indi.kider.server.bill.mapper.BillMapper;
import indi.kider.server.employee.mapper.TenantMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.InputStreamResource;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import pers.vankid.common.constant.DateTime;
import pers.vankid.common.utils.download.FileDownUtil;
import pers.vankid.common.utils.excel.ExcelExport;

import java.io.IOException;
import java.util.*;

@Service
@SuppressWarnings("ALL")
public class ExcelServiceImpl implements ExcelService {
    @Autowired
    private HousesMapper housesMapper;
    @Autowired
    private TenantMapper tenantMapper;
    @Autowired
    private BillMapper billMapper;
    /**
     * 导出租客.xlsx
     * 导出
     * @param tenantQuery
     * @return
     */
    @Override
    public ResponseEntity<InputStreamResource> exportTenant(TenantQuery tenantQuery) throws IOException {
        List<Map> tenantList = tenantMapper.getTenantList(tenantQuery);
        List<TenantExportVo> tl = new ArrayList<TenantExportVo>();
        for(Map map : tenantList){
            TenantExportVo tev = JSON.parseObject(JSON.toJSONString(map),TenantExportVo.class);
            tev.setSexShow(TenantSexTypeCons.TENANT_SEX_MAP.get(tev.getSex()));
            tev.setTypeShow(TenantTypeCons.TENANT_MAP.get(tev.getType()));
            tev.setRentStateShow(RentStateCons.MAP.get(tev.getRentState()));
            tl.add(tev);
        }
        Map<String, String> colums  = new ImmutableMap.Builder<String, String>().
                put("typeShow", "类型").
                put("sexShow", "性别").
                put("rentStateShow", "租客状态").
                put("community", "公寓名称").
                put("houseNumb", "房间号").
                put("name", "姓名").
                put("total_money", "余额").
                put("cz_money", "充值金额").
                put("phone", "电话").
                put("cardId", "身份证").
                put("formCompy", "来源企业").
                put("remark", "用户备注").
                put("rentDate", "入住日期").
                put("expireDate", "到期日期").
                put("leaveDate", "搬离日期").
                put("outOpName", "退租办理人").
                put("opOutTime", "退租办理时间").
                put("outRemark", "退租备注").
                put("cz_count", "充值条数").
                put("tx_money", "提现总金额").
                put("tx_count", "提现总条数").
                put("kk_count", "扣款条数").
                put("kk_money", "扣款总金额").
                put("yajin_money", "押金").
                build();
        Map<String, Map> columnFormatMaps = new ImmutableMap.Builder<String, Map>()
                .build();
        ExcelExport excelExport = new ExcelExport("租客信息", colums, "租客信息", tl, columnFormatMaps);
        String fileName = DateTime.FULL_DATE_NOSPLIT.format(new Date()) + Strings.nullToEmpty(excelExport.getExcelTitle()) + ".xls";
        return FileDownUtil.downloadFile(pers.vankid.common.utils.excel.ExcelUtil.export(excelExport), fileName);
    }


    /**
     * 根据房间出账单
     * @param rentBill
     * @return
     * @throws IOException
     */
    public ResponseEntity<InputStreamResource> exportBillByHouse(RentBill rentBill) throws IOException{
        List<RentBill> rentBillList = billMapper.getRentBillByHouse(rentBill);
        Map<String, String> colums  = new ImmutableMap.Builder<String, String>().
                put("community", "公寓名").
                put("address", "公寓地址").
                put("houseNumb", "房间号").
                put("eventTimeStart", "账单月份").
                put("payState", "支付状态").
                put("total", "总额")
                .build();
        Map<String, Map> columnFormatMaps = new ImmutableMap.Builder<String, Map>()
                .put("billType", BillTypeCons.STRING_MAP)
                .put("payState", PayStateCons.STRING_MAP)
                .build();
        ExcelExport excelExport = new ExcelExport("账单记录", colums, "房间账单信息", rentBillList, columnFormatMaps);
        String fileName = DateTime.FULL_DATE_NOSPLIT.format(new Date()) + Strings.nullToEmpty(excelExport.getExcelTitle()) + ".xls";
        return FileDownUtil.downloadFile(pers.vankid.common.utils.excel.ExcelUtil.export(excelExport), fileName);
    }
    /**
     * 根据租客出总账单
     * @param rentBill
     * @return
     * @throws IOException
     */
    public ResponseEntity<InputStreamResource> exportBillByTenant(RentBill rentBill) throws IOException{
        List<RentBill> rentBillList = billMapper.getRentBillByTenant(rentBill);
        Map<String, String> colums  = new ImmutableMap.Builder<String, String>().
                put("community", "公寓名").
                put("address", "公寓地址").
                put("houseNumb", "房间号").
                put("tenantName", "租客姓名").
                put("tenantTel", "租客电话").
                put("eventTimeStart", "账单月份").
                put("payState", "支付状态").
                put("total", "总额")
                .build();
        Map<String, Map> columnFormatMaps = new ImmutableMap.Builder<String, Map>()
                .put("billType", BillTypeCons.STRING_MAP)
                .put("payState", PayStateCons.STRING_MAP)
                .build();
        ExcelExport excelExport = new ExcelExport("账单记录", colums, "租客总额账单信息", rentBillList, columnFormatMaps);
        String fileName = DateTime.FULL_DATE_NOSPLIT.format(new Date()) + Strings.nullToEmpty(excelExport.getExcelTitle()) + ".xls";
        return FileDownUtil.downloadFile(pers.vankid.common.utils.excel.ExcelUtil.export(excelExport), fileName);
    }
}
